package net.sipvip.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

public class JWplayer extends Image {

	DecoratedPopupPanel popup = new DecoratedPopupPanel(true);
	final String videostr;
	final String imagestr;
	final String descriptionstr;
	final boolean controlbl;

	public JWplayer(String videostrvar, String imagestrvar,
			String descriptionvar, boolean controlvar) {
		super();
		videostr = videostrvar;
		imagestr = imagestrvar;
		descriptionstr = descriptionvar;
		controlbl = controlvar;

		sinkEvents(Event.ONCLICK);

	}

	public void onBrowserEvent(Event event) {

		switch (DOM.eventGetType(event)) {
		case Event.ONCLICK:
			// GWT.log(this.getUrl(), null);
			if (controlbl) {
				String cookies = Cookies.getCookie("user");
				String urlstr;
				if (!GWT.isScript()) {
					urlstr = "http://localhost:8080/videosservlet?cookies="
							+ cookies + "&video=" + videostr + "&image="
							+ imagestr + "&description=" + descriptionstr
							+ "&callback=";
				} else {
					// TODO deployment
					urlstr = "http://www.seksitreffit.eu/videosservlet?cookies="
						+ cookies
						+ "&video="
						+ videostr
						+ "&image="
						+ imagestr
						+ "&description="
						+ descriptionstr
						+ "&callback=";					
					

//					urlstr = "http://2.latest.wwwseksitreffiteu.appspot.com/videosservlet?cookies="
//							+ cookies
//							+ "&video="
//							+ videostr
//							+ "&image="
//							+ imagestr
//							+ "&description="
//							+ descriptionstr
//							+ "&callback=";
				}

				JsonRequests.addvideo(urlstr);
			}
			Utils.track("SipVip");
			String videoimagestr = jwplayerstr(videostr, imagestr);

			HTML player = new HTML(videoimagestr);
			popup.setWidget(player);
			popup.center();
			popup.show();

			break;

		default:
			break;
		}
	}

	String jwplayerstr(String videostr, String imgstr) {

		String playerstr = "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" width=\"656\" height=\"400\">"
				+ "<param name=\"movie\" value=\"player.swf\" />"
				+ "<param name=\"allowfullscreen\" value=\"true\" />"
				+ "<param name=\"allowscriptaccess\" value=\"always\" />"
				+ "<param name=\"flashvars\" value=\"file="
				+ videostr
				+ "&image="
				+ imgstr
				+ "\" />"
				+ "<object type=\"application/x-shockwave-flash\" data=\"player.swf\" width=\"425\" height=\"344\">"
				+ "<param name=\"movie\" value=\"player.swf\" />"
				+ "<param name=\"allowfullscreen\" value=\"true\" />"
				+ "<param name=\"allowscriptaccess\" value=\"always\" />"
				+ "<param name=\"flashvars\" value=\"file="
				+ videostr
				+ "&image="
				+ imgstr
				+ "\" />"
				+ "<p><a href=\"http://get.adobe.com/flashplayer\">Get Flash</a> to see this player.</p>"
				+ "</object>" + "</object>";

		return playerstr;

	}

}
